package com.corechina.apex_osswords.domain.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corechina.apex_osswords.domain.entity.CorePollingEquipment;
import com.corechina.apex_osswords.domain.vo.CorePollingEquipmentVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 巡检设备表 Mapper 接口
 * </p>
 *
 * @author cc
 * @since 2023-03-09
 */
@Component
public interface CorePollingEquipmentMapper extends BaseMapper<CorePollingEquipment> {

    @Delete("delete from core_polling_equipment where status = 0 and plan_id in (\n" +
            "select id from core_polling_plan where parent_id = #{pollingId} and status = 0\n" +
            ")")
    int deleteByPollingId(@Param("pollingId") Integer pollingId);

    @Delete("delete from core_polling_equipment where plan_id = #{planId}")
    int deleteByPlanId(@Param("planId") Integer planId);

    IPage<CorePollingEquipmentVo> getList(Page page, @Param("planId") Integer planId, @Param("equipmentId") Integer equipmentId, @Param("status") Integer status, @Param("type") Integer type);

    CorePollingEquipmentVo getOne(@Param("id") Integer id);

    @Update("update core_polling_equipment set status = #{status} where id = #{id}")
    int updateStatus(@Param("status") Integer status, @Param("id") Integer id);

    @Select("select count(1) from core_polling_equipment where status = #{status} and plan_id = #{planId}")
    int getStatusCount(@Param("status") Integer status, @Param("planId") Integer planId);

    @Select("select\n" +
            "\tcpe.id \n" +
            "from\n" +
            "\tcore_polling_equipment cpe\n" +
            "left join core_equipment ce on ce.id = cpe.equipment_id\n" +
            "where cpe.plan_id = #{planId} and ce.serial_number = #{serialNumber}")
    Integer getId(@Param("planId") Integer planId,@Param("serialNumber") String serialNumber);

}
